<template>
    <div class="login-container">
        <el-card class="box-card">
            <div class="login-body">
                <div class="login-title">
                    <img src="../../assets/logo.png" style="width: 45px; position: relative; top: 13px; right: 6px">
                    <span style="color: #409EFF">用户登录</span>
                </div>
                <el-form ref="form" :model="userForm">
                    <!-- 用户名 -->
                    <el-input
                        placeholder="请输入账号..."
                        v-model="userForm.userName"
                        class="login-input"
                    >
                        <template slot="prepend">
                            <div class="el-icon-user-solid"></div>
                        </template>
                    </el-input>

                    <!-- 密码 -->
                    <el-input
                        placeholder="请输入密码..."
                        v-model="userForm.password"
                        class="login-input"
                        @keyup.enter.native="login"
                        show-password
                    >
                        <template slot="prepend">
                            <div class="el-icon-lock"></div>
                        </template>
                    </el-input>

                    <!-- 按钮 -->
                    <div class="login-submit">
                        <el-button type="primary" @click="login" class="login-btn">登录</el-button>
                        <el-button type="warning" @click="$router.push('/sign-in')" class="login-btn">注册</el-button>
                    </div>
                    <div class="other-login-submit">
                        <el-button type="success" @click="$router.push('/login-admin')" class="login-btn">管理员登录</el-button>
                        <el-button type="info" @click="$router.push('/login-rider')" class="login-btn rider-btn">骑手登录</el-button>
                    </div>
                </el-form>
            </div>
        </el-card>
    </div>
</template>

<script>
export default {
    name: "login",
    data() {
        return {
            userForm: {
                userName: '', // 登录用户名
                password: ''  // 登录密码
            }
        };
    },
    methods: {
        login() {
            // 确保将 userForm 的值传递为 JSON 请求体
            const loginData = {
                userName: this.userForm.userName,
                password: this.userForm.password
            };

            this.$api.userLogin(loginData)
                .then(res => {
                    console.log("登录返回：", res);
                    if (res.code === 200) {
                        const token = res.token;
                        const userInfo = res.data;

                        if (!token) {
                            this.$message.error('登录响应中缺少 token');
                            return;
                        }

                        // 保存 token
                        localStorage.setItem('token', token);
                        this.$globalData.isLogin = true;

                        //映射头像
                        userInfo.avatar = userInfo.avatarUrl;

                        localStorage.setItem('userInfo', JSON.stringify(userInfo));  // ✅ 保存用户信息

                        // 可选：格式化时间
                        if (userInfo.signInTime) {
                            userInfo.signInTime = userInfo.signInTime.substring(0, 10);
                        }

                        // ✅ 正确写法：保留响应式
                        Object.assign(this.$globalData.userInfo, userInfo);

                        // 跳转首页
                        this.$router.replace({ path: '/index' });
                    } else {
                        this.$message.error(res.msg || '登录失败');
                    }
                })
                .catch(err => {
                    console.error("登录失败:", err);
                    this.$message.error("请求失败，请稍后重试");
                });
        },

        toIndex() {
            this.$router.replace({ path: '/index' });
        }
    }
};
</script>


<style scoped>
.login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    width: 100%;
    background: url("../../assets/login-back.png") center top / cover no-repeat;
}

.login-body {
    padding: 40px;
    width: 400px;
    height: 100%;
    opacity: 0.95;
    font-size: 30px; /* 整体字体稍微大一点 */
}

.login-title {
    padding-bottom: 30px;
    text-align: center;
    font-weight: 600;
    font-size: 26px;
    color: #409EFF;
    /* cursor: pointer; */
}

.login-input {
    margin-bottom: 20px;
}

.login-submit {
    margin-top: 20px;
    display: flex;
    justify-content: space-between;
    gap: 15px;
}

.other-login-submit {
    margin-top: 15px;
    display: flex;
    justify-content: space-between;
    gap: 15px;
}

.login-btn {
    width: 200px;
    height: 40px;
}

.rider-btn {
    background-color: #00bcd4 !important;
    border-color: #00bcd4 !important;
}

.rider-btn:hover {
    background-color: #0097a7 !important;
    border-color: #0097a7 !important;
}

.sign-in-container {
    padding: 0 10px;
}

.sign-in-text {
    color: #409EFF;
    font-size: 16px;
    text-decoration: none;
    line-height: 28px;
}

.other-submit {
    display: flex;
    justify-content: space-between;
    margin-top: 10px;
    margin-left: 0px;
}
</style>
